<template>
  <el-dialog class="pick-dialog" title="订单详情" :visible.sync="dialogVisible" :close-on-click-modal="false" width="1000px">
    <div class="form-box">
      <div class="order-info flex">
        <span>
          订单状态：
          <span class="status-txt" :style="`color:${ruleForm.orderStatus==3?'red':''}`">{{ commonMaps.orderStatusMap[ruleForm.orderStatus] }}</span>
        </span>
        <span>订单编号：{{ ruleForm.no }}</span>
        <span v-show="from=='kefu'">买家昵称：{{ ruleForm.saleChannel }}</span>
        <span v-show="from=='kefu'">卖家昵称：{{ ruleForm.supplyChannel }}</span>
        <span>创建时间：{{ ruleForm.createTime }}</span>
        <span v-if="ruleForm.orderStatus==3">超时时间：{{ ruleForm.closeTime }}</span>
        <span v-if="ruleForm.cancelTime">取消时间：{{ ruleForm.cancelTime }}</span>
      </div>
      <div class="card-box">
        <el-table :data="infoArr" border style="width: 100%">
          <el-table-column prop="goodsId" label="商品编号" width="100">
          </el-table-column>
          <el-table-column prop="gameName" label="游戏名称" width="100">
          </el-table-column>
          <el-table-column prop="qufu" width="100" label="区服">
          </el-table-column>
          <el-table-column prop="des" label="简介">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" :content="scope.row.des" placement="top-start">
                <div class="zdy-des">{{ scope.row.des }}</div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="price" width="100" label="售价" v-if="from!='seller'"></el-table-column>
          <el-table-column prop="supplyPlatformAmount" width="100" label="供货价" v-if="from=='seller'">
          </el-table-column>
        </el-table>
      </div>
      <el-descriptions style="margin-top: 15px;" title="订单信息" labelClassName="label-txt" border :column='4'>
       
        <el-descriptions-item label-class-name="itemlabel" label="商品金额" >{{ ruleForm.saleAmount }}</el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="包赔费" v-if="from=='kefu'||from=='seller'||from=='buyer2'">{{ ruleForm.guaranteeAmount }}</el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="买家服务费" v-if="ruleForm.type==3">{{ ruleForm.servicePrice }}</el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="卖家服务费" v-if="ruleForm.type==3">{{ ruleForm.supplyServicePrice }}</el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="成本" v-if="from=='kefu'||from=='seller'">{{ ruleForm.supplyAmount }}</el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="实付金额"><span style="color:red">{{ ruleForm.actualAmount }}</span></el-descriptions-item>
        <el-descriptions-item label-class-name="itemlabel" label="利润" v-if="from=='kefu'||from=='seller'" :span="3" ><span style="color:red">{{ ruleForm.profits }}</span></el-descriptions-item>
        <el-descriptions-item label="换绑手机号">{{ ruleForm.changeTel }}</el-descriptions-item>
        <!-- <el-descriptions-item label="客服">{{ ruleForm.saleUserNickname }}</el-descriptions-item> -->
        <!-- <el-descriptions-item label="需要的资料" >{{ ruleForm.profile }}</el-descriptions-item> -->
        <el-descriptions-item label="备注">
          {{ ruleForm.remark }}
        </el-descriptions-item>
        
      </el-descriptions>
      <el-descriptions style="margin-top: 15px;" v-if="sendInfo" title="发货信息" labelClassName="label-txt" border :column='1'>
        <el-descriptions-item label="换绑截图">
            <div style="min-width: 600px;">
              <img-box :img="sendInfo.profileImg"></img-box>
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="游戏账号">{{ sendInfo.accountNumber }}</el-descriptions-item>
          <el-descriptions-item label="游戏密码">{{ sendInfo.accountPassword }}</el-descriptions-item>
      </el-descriptions>

      <div class="sfk" v-if="(ruleForm.orderStatus == 3||ruleForm.orderStatus == 4)&& ruleForm.salePay == 1&&from=='buyer2'&&checkPermi(['caigou:payNow'])">
        <span>实付款：</span>
        <span class="price">
          <span>¥</span><span class="txt">{{ ruleForm.actualAmount || 0 }}</span>
        </span>
      </div>
      <div style="justify-content: flex-end;" class="flex" v-if="(ruleForm.orderStatus == 3||ruleForm.orderStatus == 4)&&ruleForm.salePay == 1&&from=='buyer2'&&checkPermi(['caigou:payNow'])">
        <div style="width: auto;">
          <payWayLsit :showYue="2" :payWay='payWay' ref="payWayLsit" @selectPayway="selectPayway"></payWayLsit>
        </div>

      </div>

      <div class="center" style="margin-top: 15px;">
        <el-button @click="close('ruleForm')">关 闭</el-button>
        <el-button v-if="(ruleForm.orderStatus == 3||ruleForm.orderStatus == 4)&&ruleForm.salePay == 1&&from=='buyer2'&&checkPermi(['caigou:payNow'])" type="primary" @click="submitForm" :loading="loading">立即支付</el-button>
      </div>
    </div>
    <PayQrcodeCommonVue ref="payQrcode" @checkOrderInterval="checkOrderInterval" @checkOrder="checkOrderHandle"></PayQrcodeCommonVue>
  </el-dialog>
</template>

<script>
import orderApi from "@/api/pickUp/order";
import PayQrcodeCommonVue from "../../moneys/components/PayQrcodeCommon.vue";
import payWayLsit from "../../components/payWayLsit.vue";
export default {
  name: "SaleOrderDetail",
  components: { PayQrcodeCommonVue, payWayLsit },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      isEdit: false,
      payQrShow: false,
      info: "",
      infoArr: [],
      ruleForm: {},
      orderId: "",
      payForm: "",
      from: "",
      payWay: "3",
      sendInfo:null
    };
  },
  created() {
    // this.orderId = this.$router.currentRoute.query.id
    // this.getDetail()
  },
  methods: {
    open(orderId, from) {
      this.from = from || "";
      this.loading = false;
      this.orderId = orderId;
      this.sendInfo = null
      this.getDetail();
      this.dialogVisible = true;
    },
    // 查询发货资料
    getSendInfos(sendId) {
      orderApi.getSaleOrderProfile(sendId).then((res) => {
        this.sendInfo = res.data;
      });
    },
    getDetail() {
      let obj = {
        id: this.orderId,
        dataType: "buy",
      };
      if (this.from == "kefu") {
        obj.dataType = "all";
      } else if (this.from == "seller") {
        obj.dataType = "sell";
      } else if (this.from == "buyer2") {
        obj.type = 2;
        this.$store.dispatch("getShopInfo");
      } else {
        obj.dataType = "buy";
      }
      orderApi.orderPayStatusHandle(obj).then((res) => {
        this.ruleForm = res.data;
        this.infoArr = [
          {
            goodsId: res.data.goodsId,
            gameName: res.data.gameName,
            qufu: res.data.qufu,
            des: res.data.des,
            price: res.data.platformAmount,
            supplyPlatformAmount: res.data.supplyPlatformAmount,
          },
        ];
        if(this.ruleForm.sendState==2){
          this.getSendInfos(this.ruleForm.id)
        }
       
      });
    },
    // 支付弹窗查询订单状态
    checkOrder() {
      let obj = {
        saleOrderId: this.orderId,
      };
      orderApi.orderPayStatusInterval(obj).then((res) => {
        // this.ruleForm = res.data
        if (res.data && res.data.order_status == 4) {
          // 支付成功
          this.$message.success("支付成功！");
          this.$store.dispatch("getShopInfo");
          this.ruleForm.orderStatus = "4";
          this.$refs.payQrcode.showSuccess("提号订单", "支付成功！");
          this.$refs.payQrcode.close();
          // this.$parent.getList()
          this.$parent.actived = "4";
          this.$parent.tabChange();
        }
      });
    },
    // 支付弹窗查询订单状态  主动
    checkOrderHandle() {
      let obj = {
        id: this.orderId,
        type: this.ruleForm.type,
      };
      orderApi.orderPayStatusHandle(obj).then((res) => {
        // this.ruleForm = res.data
        if (res.data && res.data.orderStatus == 4) {
          // 支付成功
          this.$message.success("支付成功！");
          this.$store.dispatch("getShopInfo");
          this.ruleForm.orderStatus = 4;
          this.$refs.payQrcode.showSuccess("提号订单", "支付成功！");
          this.$refs.payQrcode.close();
          // this.$parent.getList()
          this.$parent.actived = 4;
          this.$parent.tabChange();
        }
      });
    },
    //选择支付方式
    selectPayway(e) {
      this.payWay = e;
    },
    // 轮询查支付状态
    checkOrderInterval() {
      // console.log("%c Line:178 🥟 轮询查支付状态", "color:#b03734");
      this.checkOrder();
    },

    // 立即支付
    submitForm() {
      if (this.payWay == 3) {
        this.$confirm("使用余额支付会直接扣除账户余额, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.payNow();
          })
          .catch(() => {});
      } else {
        this.payNow();
      }
    },
    payNow() {
      this.loading = true;
      // 新支付参数
      let obj = {
        id: this.orderId,
        payWay: this.payWay,
        returnUrl: "",
        payScene: 3,
        amountType: 3,
        goodsTitle: "B2B商品购买" + this.ruleForm.goodsId,
        orderOther: "",
        qrPayMode: 4,
        qrcodeWidth: 200,
        type: "2",
      };

      orderApi
        .payNow(obj)
        .then((res) => {
          //console.log("%c Line:38 🍫 res", "color:#2eafb0", res);
          this.loading = false;
          if (res.code == 200) {
            this.$refs.payQrcode.open(
              this.payWay,
              res.data.formStr,
              res.data.isHuifu
            );
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm = {
        tel: "",
        remark: "",
        ziliao: "",
        shopprice: this.info.newPrice,
        price: this.info.price,
      };
      this.checkList = [];
      this.ruleForm.id = this.info.id;
    },
    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.form-box {
  padding: 15px;
  // width: 1000px;
}
::v-deep .itemlabel {
  width: 100px !important;
}

.title-box {
  // justify-content: space-between;
  font-size: 14px;
  line-height: 40px;
  padding: 0 15px;
}

.order-info {
  box-sizing: border-box;
  // height: 50px;
  background: #eee;
  padding: 15px;
  margin-bottom: 10px;
  flex-wrap: wrap;
  line-height: 26px;

  > span {
    margin-right: 20px;
  }
}

.card-box {
  padding: 0;
}

.zdy-des {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  margin: 6px 0;
}

.price-box {
  text-align: right;
  border-bottom: 1px solid #eee;
  margin-bottom: 15px;
  padding-right: 20px;
}

.sfk {
  text-align: right;
  padding-right: 20px;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 10px;
  margin-top: 10px;

  .txt {
    font-size: 26px;
    color: #ff0000;
    margin-left: 5px;
  }
}

.demo-ruleForm {
  padding-right: 20px;
}



.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
